package com.portal.back.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.portal.back.service.ISysProfessionalService;
import com.portal.back.service.ISysReSearchSerivce;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysProfessional;
import com.portal.domain.SysReSearch;

public class SysReSearchService implements ISysReSearchSerivce {
     private  IBaseDao<SysReSearch,Integer> dao;
     private  ISysProfessionalService  service;

     public  SysReSearchService(){
    	 dao=new BaseDao<SysReSearch, Integer>(SysReSearch.class);
    	 service=new SysProfessionalService();
     }
     public  boolean  saveReSearch(SysReSearch search){
    		if (search == null)
    			return false;
    		String sql = "";
    		try {
    			if (search.getId() == null) {
    				sql = "insert into sys_research(title,attachUrl,categoryId) values(?,?,?)";
    				dao.insert(sql, new Object[] { search.getTitle(),search.getAttachUrl(),search.getCategoryId()});
    						
    			} else {
    				sql = " update sys_research set title=?,attachUrl=?,categoryId=?  where id=? ";
    				dao.update(sql,new Object[] {search.getTitle(),search.getAttachUrl(),search.getCategoryId(),search.getId()});			
    			}

    			return true;
    		} catch (Exception e) {
    			e.printStackTrace();
    			return false;
    		}
     }
     public  List<SysReSearch> getlist(){
    		String sql = "select * from sys_research  ";
    		List<SysReSearch> list = dao.findList(sql);
    		if (list != null && list.size() > 0) {
    			List<SysReSearch> newlist = new ArrayList<SysReSearch>();
    			for (int i = 0; i < list.size(); i++) {
    				SysReSearch search = list.get(i);
                    SysProfessional pro= service.getById(search.getCategoryId().toString());
    				search.setCategoryString(pro.getTitle());
    				newlist.add(search);
    			}
    			return newlist;
    		}
    	 return null;
     }
 	public SysReSearch getById(String id) {
		String sql = "select * from sys_research where  id=?";
		List<SysReSearch> list = dao.findList(sql, new Object[] { id });
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
 	public boolean deleteById(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "delete from sys_research where  id=?";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from sys_research where  id=" + delteid[0];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

}
